package com.vainycos.lotterysearch.controller;

import com.vainycos.lotterysearch.model.DoubleColorBall;
import com.vainycos.lotterysearch.model.base.Result;
import com.vainycos.lotterysearch.model.ro.DoubleColorRo;
import com.vainycos.lotterysearch.service.DoubleColorService;
import com.vainycos.lotterysearch.service.notify.DingNotify;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: Vainycos
 * @description
 * @date: 2022/11/3 14:53
 */
@RestController
@RequestMapping("/doubleColor")
public class DoubleColorController {

    @Autowired
    private DoubleColorService doubleColorService;

    @Autowired
    private DingNotify dingNotify;

    /**
     * 查询某一期及之后所有的开奖情况
     * @param periodId
     * @return
     * @throws Exception
     */
    @GetMapping("/searchBallWithinPeriodId")
    public Result<List<DoubleColorBall>> searchBallWithinPeriodId(int periodId) {
        List<DoubleColorBall> list = doubleColorService.getLaterBLB(periodId);
        return Result.of(list);
    }

    /**
     * 查询单期开奖情况
     * @param periodId
     * @throws Exception
     */
    @GetMapping("/searchBallBySinglePeriodId")
    public Result<DoubleColorBall> searchBallBySinglePeriodId(int periodId) {
        return Result.of(doubleColorService.getDCBBySinglePeriodId(periodId));
    }

    /**
     * 查询最近一期开奖情况
     */
    @GetMapping("/searchLatest")
    public Result<DoubleColorBall> searchLatest() {
        return Result.of(doubleColorService.searchLatestDoubleColor());
    }

    /**
     * 输入号码匹配最近一期中奖情况
     * @param doubleColorRo 输入号码
     * @return
     */
    @PostMapping("/matchingResultWithLatest")
    public Result<String> matchingResultWithLatest(@RequestBody DoubleColorRo doubleColorRo) {
        StringBuffer result = new StringBuffer();
        DoubleColorBall latestResult = doubleColorService.searchLatestDoubleColor();
        int i = doubleColorService.matchLevelPrizes(doubleColorRo, latestResult);
        result.append("期号：" + latestResult.getPeriodId() + "。"
                + "开奖日期：" + latestResult.getLotteryDate() + "。"
                + "开奖号码：" + latestResult.winnerResult() + "。"
                + "您的号码：" + doubleColorRo.inputNumberResult() + "。");
        if (i > 0){
            result.append("恭喜您中奖了，中了" + i + "等奖");
        }else {
            result.append("不好意思您未中奖，希望下次好运");
        }
        return Result.of(result.toString());
    }

    /**
     * 输入号码根据传入periodId匹配对应期数中奖情况
     * @param doubleColorRo 输入号码
     * @return
     */
    @PostMapping("/matchingResultWithPeriodId")
    public Result<String> matchingResultWithPeriodId(@RequestBody DoubleColorRo doubleColorRo) {
        return Result.of(doubleColorService.describeByPrice(doubleColorService.getDCBBySinglePeriodId(doubleColorRo.getPeriodId()), doubleColorRo));
    }

    /**
     * 全量初始化新增
     * @return
     */
    @GetMapping("/batchInsertInit")
    public Result batchInsertInit(){
        return Result.of(doubleColorService.batchInsertInit());
    }

    /**
     * 同步增量更新
     * @return
     */
    @GetMapping("/syncInsert")
    public Result syncInsert(){
        return Result.of(doubleColorService.syncInsert());
    }

    /**
     * 前区号码匹配历史中奖率
     * @param frontAreaNum 前区号码
     * @return
     */
    @GetMapping("/historyFrontArea")
    public Result historyFrontArea(int frontAreaNum){
        String result = "";
        String s = doubleColorService.historyFrontArea(frontAreaNum);
        result += frontAreaNum + "->该前区号码历史中奖率为" + s;
        return Result.of(result);
    }

    /**
     * 后区号码匹配历史中奖率
     * @param endAreaNum 后区号码
     * @return
     */
    @GetMapping("/historyEndArea")
    public Result historyEndArea(int endAreaNum){
        String result = "";
        String s = doubleColorService.historyEndArea(endAreaNum);
        result += endAreaNum + "->该后区号码历史中奖率为" + s;
        return Result.of(result);
    }

    /**
     * 统计前区1-33数字的历史中奖率
     */
    @GetMapping("/statisticsFront")
    public Result statisticsFront(){
        String result = "";
        for (int i = 1; i < 34; i++) {
            String s = doubleColorService.historyFrontArea(i);
            result += i + "->该前区号码历史中奖率为" + s + " \n";
        }
        return Result.of(result);
    }

    /**
     * 统计后区1-16数字的历史中奖率
     */
    @GetMapping("/statisticsEnd")
    public Result statisticsEnd(){
        String result = "";
        for (int i = 1; i < 17; i++) {
            String s = doubleColorService.historyEndArea(i);
            result += i + "->该后区号码历史中奖率为" + s + " \n";
        }
        return Result.of(result);
    }

    @GetMapping("/dingNotifyStatistics")
    public Result dingNotifyStatistics(){
        String result = "双色球历史号码统计\n";
        for (int i = 1; i < 34; i++) {
            String s = doubleColorService.historyFrontArea(i);
            result += i + "->该前区号码历史中奖率为" + s + " \n";
        }
        result += "--- \n";
        for (int i = 1; i < 17; i++) {
            String s = doubleColorService.historyEndArea(i);
            result += i + "->该后区号码历史中奖率为" + s + " \n";
        }
        dingNotify.notifyMarkdown("双色球历史号码统计", result);
        return Result.of(true);
    }
}
